/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.web.manager.users;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.hutech09.jsale.bsn.IAccessUserManager;
import org.hutech09.jsale.bsn.exception.ItemExistsException;
import org.hutech09.jsale.entity.AccessUser;

/**
 *
 * @author Nam
 */
@ManagedBean
@RequestScoped
public class NewUserCtrl {

    @EJB
    private IAccessUserManager userMgr;

    private AccessUser user = new AccessUser();

    public NewUserCtrl() {
    }

    public AccessUser getUser() {
        return user;
    }

    public void setUser(AccessUser user) {
        this.user = user;
    }

    public void create() {
        try {
            userMgr.create(user);
            user = new AccessUser();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "User is created!", ""));
        } catch (ItemExistsException e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "User ID is existed!", ""));
            System.out.println("####" + e.getClass());
        }
    }

    public void clear() {
        System.out.println(user);
        user = new AccessUser();
    }
}
